unit u_partic;

interface

uses
 sysutils,
 system.Drawing,
Microsoft.DirectX,
Microsoft.DirectX.Direct3D,
Microsoft.DirectX.Direct3DX;

type

TColor = record
  R, G, B : Double;
end;

particule = record
    X, Y,z, to_x, to_y : single;
    delta_x, delta_y,delta_z : single;
    l:boolean;
end;

TParticle = class
    private
      particle_mesh : Mesh;
      particle_texture,part,texture1,texture2,texture_lvl : Texture;
      t_tab_part,t_tab_part_enne, t_tab_part_lvl : Array of particule;
      
    public
      constructor Create(dev : device);
      procedure Render_player(dev:device);
      procedure Render_ennemi(dev:device);
      destructor Destroy; override;
      procedure add_part_player(nbr, x, y, vx, vy : integer);
      procedure add_part_ennemi(nbr, x, y, vx, vy : integer);
      procedure render_level_up(dev:device);
      procedure add_part_lvlup(nbr, x, y, vx, vy : integer);
end;


implementation

constructor TParticle.Create(dev : device);
var
  i:integer;
  begin
    inherited create;
    SetLength(t_tab_part, 0);
    SetLength(t_tab_part_enne, 0);
    particle_mesh := mesh.FromFile('meshes\carre.x', meshflags.SystemMemory, dev);
    particle_texture := Textureloader.FromFile(dev, 'meshes\particle.bmp');
    part := Textureloader.FromFile(dev, 'meshes\pipi.bmp');
    texture1:= Textureloader.FromFile(dev, 'meshes\texture.JPG');
    texture2:= Textureloader.FromFile(dev, 'meshes\texture2.JPG');
    texture_lvl := Textureloader.FromFile(dev, 'meshes\texture2.JPG');
  end;

procedure tparticle.add_part_player(nbr, x, y, vx, vy : integer);
var
  i, j : integer;
  len : integer;
  v_tmp : single;
  begin
    len := nbr;
    SetLength(t_tab_part, len );
    for i := 0 to len - 1 do
      begin
        v_tmp := (Random(50) + 25);
        t_tab_part[i].X := x;
        t_tab_part[i].Y := y;
        t_tab_part[i].z := 0.6;
        if (vx-x)>0 then
          t_tab_part[i].to_x := vx+0.5
        else
          t_tab_part[i].to_x := vx-0.5;
        if (vy-y)>0 then
          t_tab_part[i].to_y := vy+0.5
        else
          t_tab_part[i].to_y := vy-0.5;
        t_tab_part[i].l:= true;
          t_tab_part[i].delta_x := (vx - x)/v_tmp ;


          t_tab_part[i].delta_y := (vy - y)/v_tmp;
          t_tab_part[i].delta_z := 0;

      end;
  end;

procedure tparticle.add_part_ennemi(nbr, x, y, vx, vy : integer);
var
  i, j : integer;
  len : integer;
  v_tmp : single;
  begin
    len := nbr;
    SetLength(t_tab_part_enne, len );
    for i := 0 to len - 1 do
      begin
        v_tmp := (Random(50) + 25);
        t_tab_part_enne[i].X := x;
        t_tab_part_enne[i].Y := y;
        t_tab_part_enne[i].z := 0.6;
        if (vx-x)>0 then
          t_tab_part_enne[i].to_x := vx+0.5
        else
          t_tab_part_enne[i].to_x := vx-0.5;
        if (vy-y)>0 then
          t_tab_part_enne[i].to_y := vy+0.5
        else
          t_tab_part_enne[i].to_y := vy-0.5;
          t_tab_part_enne[i].l:= true;
          t_tab_part_enne[i].delta_x := (vx - x)/v_tmp ;


          t_tab_part_enne[i].delta_y := (vy - y)/v_tmp;
          t_tab_part_enne[i].delta_z := 0;

      end;
  end;

procedure TParticle.Render_player(dev:device);
var
  i, j : integer;
  k:single;
  l:boolean;
  len : integer;
  begin
  len := Length(t_tab_part);
  i := 0;
  k := 0.00003;
  while i < len do
    begin

      dev.Transform.World :=
                    dev.Transform.World.Translation(t_tab_part[i].x,t_tab_part[i].z,t_tab_part[i].y);
      if t_tab_part[i].l then
        dev.SetTexture(0, particle_texture)
      else
        dev.SetTexture(0,part);
      particle_mesh.DrawSubset(0);

    t_tab_part[i].x:=t_tab_part[i].x + t_tab_part[i].delta_x ;
    t_tab_part[i].y:=t_tab_part[i].y + t_tab_part[i].delta_y ;
    t_tab_part[i].z:=t_tab_part[i].z+ t_tab_part[i].delta_z + k -0.002;
    k:=-k;


        if (t_tab_part[i].z<0.1)or (t_tab_part[i].z>2.5) then
          begin
            t_tab_part[i]:=t_tab_part[len-1];
           len:=len-1;
           SetLength(t_tab_part,len);
          end
        else
          begin
            if (t_tab_part[i].l)and((trunc(t_tab_part[i].x-t_tab_part[i].to_x)=0)and(trunc(t_tab_part[i].y-t_tab_part[i].to_y)=0)) then
              begin
                t_tab_part[i].delta_x:=(random(600)-300)/100000;
                t_tab_part[i].delta_y:=(random(600)-300)/100000;

                if t_tab_part[i].z>2 then
                  begin
                    t_tab_part[i].l:=false;
                  end
                else
                  t_tab_part[i].delta_z:=random(10)/10;
               end;
            if not(t_tab_part[i].l) then
                t_tab_part[i].delta_z := -t_tab_part[i].delta_z - 0.03;
                i:=i+1;

          end;
      end;
    end;

procedure TParticle.Render_ennemi(dev:device);
var
  i, j : integer;
  k:single;
  l:boolean;
  len : integer;
begin
  len := Length(t_tab_part_enne);
  i := 0;
  k := 0.003;
  while i < len do
    begin

      dev.Transform.World :=
                    dev.Transform.World.Translation(t_tab_part_enne[i].x,t_tab_part_enne[i].z,t_tab_part_enne[i].y);
      if t_tab_part_enne[i].l then
        dev.SetTexture(0, texture1)
      else
        dev.SetTexture(0,texture2);
      particle_mesh.DrawSubset(0);

      //particle_mesh.DrawSubset(0);

    t_tab_part_enne[i].x:=t_tab_part_enne[i].x + t_tab_part_enne[i].delta_x;
    t_tab_part_enne[i].y:=t_tab_part_enne[i].y + t_tab_part_enne[i].delta_y;
    t_tab_part_enne[i].z:=t_tab_part_enne[i].z + t_tab_part_enne[i].delta_z + k -0.06;
    k:=-k;


        if (t_tab_part_enne[i].z<0.1)or (t_tab_part_enne[i].z>2.5) then
          begin
            t_tab_part_enne[i]:=t_tab_part_enne[len-1];
            len:=len-1;
            SetLength(t_tab_part_enne,len);
          end
        else
          begin
            if (t_tab_part_enne[i].l)and((trunc(t_tab_part_enne[i].x-t_tab_part_enne[i].to_x)=0)and(trunc(t_tab_part_enne[i].y-t_tab_part_enne[i].to_y)=0)) then
              begin
                t_tab_part_enne[i].delta_x:=(random(600)-300)/100000;
                t_tab_part_enne[i].delta_y:=(random(600)-300)/100000;
               end;
            if not(t_tab_part_enne[i].l) then
                t_tab_part_enne[i].delta_z := -t_tab_part_enne[i].delta_z - 0.03;
                i:=i+1;

          end;
      end;
    end;


destructor TParticle.Destroy;
begin
end;

procedure tparticle.render_level_up(dev : device);
var
  i, j : integer;
  k:single;
  l:boolean;
  len : integer;
begin
  len := Length(t_tab_part_lvl);
  i := 0;
  k := 0.003;
  while i < len do
    begin

      dev.Transform.World :=
                    dev.Transform.World.Translation(t_tab_part_lvl[i].x,t_tab_part_lvl[i].z,t_tab_part_lvl[i].y);

        dev.SetTexture(0, texture_lvl);
      particle_mesh.DrawSubset(0);



    t_tab_part_lvl[i].x:=t_tab_part_lvl[i].x + t_tab_part_lvl[i].delta_x +(random(15)-7.5)/300;
    t_tab_part_lvl[i].y:=t_tab_part_lvl[i].y + t_tab_part_lvl[i].delta_y +(random(15)-7.5)/300;
    t_tab_part_lvl[i].z:=t_tab_part_lvl[i].z + t_tab_part_lvl[i].delta_z + k+(random(15)-7.5)/300 ;
    k:=-k;
    if t_tab_part_lvl[i].z>4 then
      begin
        t_tab_part_lvl[i]:=t_tab_part_lvl[length(t_tab_part_lvl)-1];
        len:=len-1;
        setlength(t_tab_part_lvl, length(t_tab_part_lvl)-1);
      end
    else
      i := i+1;
    end;
end;

procedure tparticle.add_part_lvlup(nbr, x, y, vx, vy : integer);
var
  i, j : integer;
  len : integer;
  v_tmp : single;
  begin
    len := nbr;
    SetLength(t_tab_part_lvl, len );
    for i := 0 to len - 1 do
      begin
        v_tmp := (Random(50) + 25);
        t_tab_part_lvl[i].X := x;
        t_tab_part_lvl[i].Y := y;
        t_tab_part_lvl[i].z := 0;
        if (vx-x)>0 then
          t_tab_part_lvl[i].to_x := vx+0.5
        else
          t_tab_part_lvl[i].to_x := vx-0.5;
        if (vy-y)>0 then
          t_tab_part_lvl[i].to_y := vy+0.5
        else
          t_tab_part_lvl[i].to_y := vy-0.5;
          t_tab_part_lvl[i].l:= true;
          t_tab_part_lvl[i].delta_x := 0 ;


          t_tab_part_lvl[i].delta_y := 0;
          t_tab_part_lvl[i].delta_z := 0.03;

      end;
      i:=1;
  end;

end.
